using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Test
{
    public class TestExecutor<T>
    {
        Tester<T> tester;
        ITest<T> test;
        TextWriter textWriter;
        List<long> elapseds;

        public ITest<T> Test
        {
            get { return this.test; }
        }
        public List<long> Elaspeds
        {
            get { return this.elapseds; }
        }


        public TestExecutor(ITest<T> test, Tester<T> tester) : this(test, null, tester) { }
        public TestExecutor(ITest<T> test, TextWriter textWriter, Tester<T> tester)
        {
            this.test = test;
            this.textWriter = textWriter;
            this.elapseds = new List<long>();
            this.tester = tester;
        }

        public long ExecuteTest(T p)
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Reset();
            stopWatch.Start();
            string result = test.Run(p);
            stopWatch.Stop();
            if (textWriter != null)
            {
                textWriter.WriteLine(test.Name + ":");
                textWriter.WriteLine(result);
            }
            long elapsed = 0;
            switch (tester.Unit)
            {
                case ElapsedUnit.Milliseconds:
                    elapsed = stopWatch.ElapsedMilliseconds;
                    break;
                case ElapsedUnit.Ticks:
                    elapsed = stopWatch.ElapsedTicks;
                    break;
                default:
                    break;
            }
            this.elapseds.Add(elapsed);
            return elapsed;
        }
    }
}
